package com.dissertation.action;

import com.alibaba.fastjson.JSON;
import com.dissertation.model.Participant;
import com.dissertation.model.StudentParticipant;
import com.dissertation.model.TeacherParticipant;
import com.dissertation.pojo.Message;
import com.dissertation.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

@RestController
@RequestMapping("participant")
public class ParticipantController {

    @Autowired
    ParticipantService participantService;


    /**
     * 添加学生参与者
     * */
    @PostMapping("student")
    Message addStudent(@RequestBody @Validated StudentParticipant studentParticipant , BindingResult result){
        if(result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            return Message.createErr( JSON.toJSONString( fieldErrors.stream()
                    .map( DefaultMessageSourceResolvable::getDefaultMessage )
                    .collect( toList() ) ) );
        }
        return Message.createSuc(participantService.addStudent(studentParticipant));
    }


    /**
     * 修改学生参与者信息
     * */
    @PutMapping("changeStudent")
    Message changeStudent(@RequestBody @Validated StudentParticipant studentParticipant , BindingResult result){
        if(result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            return Message.createErr( JSON.toJSONString( fieldErrors.stream()
                    .map( DefaultMessageSourceResolvable::getDefaultMessage )
                    .collect( toList() ) ) );
        }
        return Message.createSuc(participantService.addStudent(studentParticipant));
    }
    /**
     * 添加教师参与者
     * */
    @PostMapping("teacher")
    Message addTeacher(@RequestBody @Validated TeacherParticipant teacherParticipant , BindingResult result){
        if(result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            return Message.createErr( JSON.toJSONString( fieldErrors.stream()
                    .map( DefaultMessageSourceResolvable::getDefaultMessage )
                    .collect( toList() ) ) );
        }
        return Message.createSuc(participantService.addTeacher(teacherParticipant));
    }
    /**
     * 修改教师参与者信息
     * */
    @PutMapping("changeTeacher")
    Message changeTeacher(@RequestBody @Validated StudentParticipant studentParticipant , BindingResult result){
        if(result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            return Message.createErr( JSON.toJSONString( fieldErrors.stream()
                    .map( DefaultMessageSourceResolvable::getDefaultMessage )
                    .collect( toList() ) ) );
        }
        return Message.createSuc(participantService.addStudent(studentParticipant));
    }
    /**
     * 根据姓名查找学生（本学院）
     * */
    @GetMapping("student")
    Message searchStudentByName(@RequestParam String name, @RequestParam String schoolName){
        return participantService.searchStudentByName(name, schoolName);
    }

    /**
     * 根据姓名查找学生（其他学院）
     * */
    @GetMapping("student/other")
    Message searchStudentNoName(@RequestParam String name, @RequestParam String schoolName){
        return participantService.searchStudentNoName(name, schoolName);
    }
    /**
     * 导入学生用户
     * */
    @PutMapping("student")
    Message importStudentParticipant(@RequestParam List<MultipartFile> files){
        return Message.createSuc(participantService.importStudentParticipant(files));
    }

    /**
     * 根据姓名查找教师（本学院）
     * */
    @GetMapping("teacher")
    Message searchTeacherByName(@RequestParam String name, @RequestParam String schoolName){
        return participantService.searchTeacherByName(name, schoolName);
    }

    /**
     * 根据姓名查找教师（其他学院）
     * */
    @GetMapping("teacher/other")
    Message searchTeacherNoName(@RequestParam String name, @RequestParam String schoolName){
        return participantService.searchTeacherNoName(name, schoolName);
    }

    /**
     * 导入教师用户
     * */
    @PutMapping("teacher")
    Message importTeacherParticipant(@RequestParam List<MultipartFile> files){
        return Message.createSuc(participantService.importTeacherParticipant(files));
    }

    /**
     * 删除参与者
     * */
    @DeleteMapping
    Message deleteParticipant(@RequestParam String[] ids){
        List<String> idList = Arrays.asList(ids);
        return  Message.createSuc(participantService.deleteParticipant(idList));
    }

    @GetMapping
    Message searchAllParticipant(){
        return Message.createSuc(participantService.searchAllParticipant());
    }

    @GetMapping("uniqueName")
    Message searchByName(@RequestParam String name){
    return Message.createSuc(participantService.searchByName(name));
    }

}
